/*
 * file: symbol.c
 */

#include <string.h>
#include "symbol.h"

char lexemes[STRMAX];
int lastchar = -1;
int lastentry = 0;

int
lookup(char *s)
{
    int i;

    for (i = lastentry; i > 0; i--)
        if (strcmp(symtable[i].lexptr, s) == 0)
            return i;

    return 0;
}

int
insert(char *s, int tok)
{
    int len;

    len = strlen(s);

    if (lastentry >= SYMMAX - 1)
        error("symbol table full");
    if (lastchar + len >= STRMAX - 1)
        error("lexemes array full");

    lastentry++;
    symtable[lastentry].token = tok;
    symtable[lastentry].lexptr = &lexemes[lastchar + 1];
    lastchar = lastchar + len + 1;
    strcpy(symtable[lastentry].lexptr, s);

    return lastentry;
}

